/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Courses;
import vn.webapp.model.Student;
import vn.webapp.model.Students;
import vn.webapp.model.Users;

@Repository("studentsDAO")
//@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class StudentsDAOImpl extends BaseDao implements StudentsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    @Transactional
    public List<Students> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Students.class, "student");
            criteria.createAlias("student.classes", "classes");            
            //criteria.setMaxResults(10);
            //criteria.setFirstResult(0);
            List<Students> classes = criteria.list();
            commit();
            return classes;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Students viewDetail(int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Students c WHERE c.id = '" + id + "'");
        return (Students) q.uniqueResult();
    }

    @Override
    public Students checkExistsCode(String code, int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Students c WHERE c.code = '" + code + "' AND c.id != '" + id + "'");
        return (Students) q.uniqueResult();
    }

    @Override
    public int removeStudent(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Students c WHERE c.id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }

    @Override
    public int updateTheStudent(String code, String name, int classid, int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Students SET code ='" + code + "', name = '" + name + "', classid = '" + classid + "'  WHERE id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int saveAStudent(Students student)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(student);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    @Transactional
    public List<Students> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Students.class, "student");
            criteria.createAlias("student.classes", "classes");
            //Criteria criteria = getSession().createCriteria(Students.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Students> students = criteria.list();
            commit();
            return students;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Students.class);            
            List<Students> students = criteria.list();
            commit();
            return students.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
}
